<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AgentRewardRuleSettings;
use Illuminate\Http\Request;

class RewardSettingsController extends Controller
{
    public function table()
    {
        return $this->response->array(
            AgentRewardRuleSettings::query()->with(['operator'])->get()->toArray()
        );
    }

    public function edit(Request $request, $id)
    {
        $this->validate($request, [
            'threshold_amount' => ['required', 'integer', 'min:1'],
            'reward' => ['required', 'integer', 'min:1']
        ], [
            'threshold_amount.required' => '门槛金额不能为空',
            'threshold_amount.integer' => '门槛金额不合法',
            'threshold_amount.min' => '门槛金额最少一分钱',
            'reward.required' => '奖励不能为空',
            'reward.integer' => '奖励不合法',
            'reward.min' => '奖励最少一分钱',
        ]);

        $setting = AgentRewardRuleSettings::query()->findOrFail($id);
        $setting->update([
            'threshold_amount' => $request->get('threshold_amount'),
            'reward' => $request->get('reward'),
            'operator_id' => $this->user['id']
        ]);

        // 清理缓存
        \App\Services\AgentRewardRuleSettings::forgetSettingsCache();

        return $this->response->noContent();
    }
}
